#include "nrf24.h"

#include "ch32f103_nrf24.h"

static nrf24_port_t* nrf24_port;

static nrf24_t nrf24;

//static void nrf24_write_register(uint8_t addr, uint8_t data)
//{
//	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_ENABLE, NULL);
//	nrf24.staus = nrf24_port->transfer(0x01);
//	nrf24_port->transfer(0x02);
//	nrf24_port->transfer(0x03);
//	nrf24_port->transfer(0x04);
//	nrf24_port->transfer(0x05);
//	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_DISABLE, NULL);
//}

uint8_t nrf24_read_config()
{
	uint8_t data;
	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_ENABLE, NULL);
	nrf24.staus = nrf24_port->transfer( R_REGISTER | CONFIG );
	data = nrf24_port->transfer(NOP);
	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_DISABLE, NULL);
	return data;
}

uint8_t nrf24_read_en_aa()
{
	uint8_t data;
	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_ENABLE, NULL);
	nrf24.staus = nrf24_port->transfer( R_REGISTER | EN_AA );
	data = nrf24_port->transfer(NOP);
	nrf24_port->ioctl(NRF24_PORT_CMD_CSN_DISABLE, NULL);
	return data;
}

static void nrf24_test()
{
	printf("status:0x%02x  config:0x%02x\n", nrf24.staus, nrf24_read_config());
	printf("status:0x%02x  en_aa:0x%02x\n", nrf24.staus, nrf24_read_en_aa());
}

static void nrf24_init()
{
	// Get nrf24 port.
	nrf24_port = get_nrf24_port();
	nrf24_port->init();
	
	// Use default values to config NRF24L01 register.
}

static void nrf24_deinit()
{
	nrf24_port->deinit();
}

nrf24_t* get_nrf24(void)
{
	nrf24.init = nrf24_init;
	nrf24.deinit = nrf24_deinit;
	nrf24.test = nrf24_test;
	
	return &nrf24;
}
